package com.tanhua.admin.service;

import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.vo.DistributionVo;
import com.tanhua.model.vo.SummaryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

@Service
public class SummaryService {

    @DubboReference
    private UserApi userApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public SummaryVo summary() {
        SummaryVo vo = new SummaryVo();

        //累计用户
        Integer userCount = userApi.userCount();
        vo.setCumulativeUsers(userCount);

        //过去30天活跃用户
        Integer activeMonth = userApi.finActivityForDays(30);
        vo.setActivePassMonth(activeMonth);

        //过去7天活跃用户
        Integer activeWeek = userApi.finActivityForDays(7);
        vo.setActivePassWeek(activeWeek);

        //今日新增用户
        Integer newUserCount = userApi.newUserCount(0);
        vo.setNewUsersToday(newUserCount);

        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer newUserCountLast = userApi.newUserCount(-1);
        if (newUserCountLast != 0) {
            int newUserRate = (newUserCount - newUserCountLast) / newUserCount * 100;
            vo.setNewUsersTodayRate(newUserRate);
        }

        //今日登录次数
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new GregorianCalendar().getTime());
        String value = redisTemplate.opsForValue().get(Constants.Login_Times_Num + date);
        Integer loginNow = 0;
        if (value != null) {
             loginNow = Integer.valueOf(value);
            vo.setLoginTimesToday(Integer.valueOf(value));
        }

        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Calendar calendar = new GregorianCalendar();
        calendar.add(calendar.DATE,-1);
        String dateLast = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String valueLast = redisTemplate.opsForValue().get(Constants.Login_Times_Num + dateLast);
        if (valueLast!=null) {
            Integer loginLast = Integer.valueOf(valueLast);
            int loginRate = (loginNow - loginLast) / loginNow * 100;
            vo.setLoginTimesTodayRate(loginRate);
        }
        //今日活跃用户
        Integer activeDay = userApi.finActivityForDays(0);
        vo.setActiveUsersToday(activeDay);

        //昨日活跃用户
        Integer activeLast = userApi.finActivityForDays(-1);
        vo.setActiveUsersYesterday(activeLast);

        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        if (activeDay!=0) {
            int activeDayRate = (activeDay - activeLast) / activeDay * 100;
            vo.setActiveUsersTodayRate(activeDayRate);
        }

        //过去7天平均日使用时长，单位秒
        Long useTimePassWeek = userApi.getUseTimePassWeek();
        vo.setUseTimePassWeek(Integer.valueOf(useTimePassWeek.toString()));

        //昨日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer activeLastTwo = userApi.finActivityForDays(-2);
        if (activeLast!=0) {
            int activeLastRate = (activeLast - activeLastTwo) / activeLast * 100;
            vo.setActiveUsersYesterdayRate(activeLastRate);
        }

        return vo;
    }

    public HashMap newUserCount(LocalDate start, LocalDate end) {
        return userApi.newUserCount(start, end);
    }

    public HashMap activeUserCount(LocalDate start, LocalDate end) {
        return userApi.activeUserCount(start, end);
    }

    public HashMap retainedUserCount(LocalDate start, LocalDate end) {
        return userApi.retainedUserCount(start, end);
    }


    public HashMap getDistribution(LocalDate start, LocalDate end) {
        //行业分布TOP10
        List<DistributionVo> industryDistribution = userApi.getIndustry(start, end);

        //年龄分布
        List<DistributionVo> ageDistribution = userApi.getAgeRange(start, end);

        //性别分布
        List<DistributionVo> genderDistribution = userApi.getGenderRange(start, end);

        //地区分布
        List<DistributionVo> localDistribution = userApi.getLocalRange(start, end);

        //地区合计
        List<DistributionVo> localTotal = userApi.getLocalTotal(start, end);

        HashMap ret = new HashMap();
        ret.put("industryDistribution", industryDistribution);
        ret.put("ageDistribution", ageDistribution);
        ret.put("genderDistribution", genderDistribution);
        ret.put("localDistribution", localDistribution);
        ret.put("localTotal", localTotal);
        return ret;
    }
}
